<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Queue\Capsule\Manager;
use Session;
use DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Auth;
use App\CurrentDay;
use App\BankShop;
use App\UserCoupon;

class CurrentDay1Controller extends Controller
{
	public function __construct()
	{
		header("Access-Control-Allow-Origin:*"); //*号表示所有域名都可以访问
	}

	public function index(){
//		$var=sprintf("%02d", 2);//生成4位数，不足前面补0
//		echo $var;//结果为0002
//		exit;
		// 最近7天的数据
//		$curent_week = array();
//		$week_start = date('Y-m-d', strtotime('-6 days'));
//		$week_end = date('Y-m-d', time());
//		$i_ct = round((strtotime($week_end)-strtotime($week_start))/3600/24); // 计算开始到结束之间的天数
//		for($i=0;$i<=$i_ct;$i++){
//			if($i==0){
//				$curent_week[$i] = $week_start;
//			}
//			$curent_week[$i] = date('Y-m-d',strtotime($week_start."+".$i." day"));
//		}
		$current_hour = array();
		for ($i=0;$i<=24;$i++){
			if($i==0){
				$current_hour[$i] = $i;
			}else{
				$current_hour[$i] = sprintf("%02d", ($current_hour[$i-1] + 1));
			}
		}
		dd($current_hour);


	}

	// 获取折线图数据
	public function getMoneyChartData(){
		$dataArr = array(); // 日期数组
		$moneyArr = array();   // 金额数组

		$orderMoneyTotal  = CurrentDay::getOrderMoneyDay();
		 // 折线图数据：开始、结束 日期数组
		$current_hour = array();
		for ($i=0;$i<24;$i++){
			if($i==0){
				$current_hour[$i] = $i;
			}else{
//					$current_hour[$i] = sprintf("%02d", ($current_hour[$i-1] + 1));
				$current_hour[$i] = $current_hour[$i-1] + 1;
			}
		}
//			dd($current_hour);

		if($orderMoneyTotal){
			$moneyTotalArr = array(); // 折线图数据：获取数
			foreach($orderMoneyTotal as $key=>$value){
				// 拼接获取数、消耗数 相关数组
				$moneyTotalArr[$key]['times'] = isset($value->times) ? $value->times : 0;   // 时间
				$moneyTotalArr[$key]['xdTotalMoney'] = isset($value->xdTotalMoney) ? $value->xdTotalMoney : 0;   // 下单总金额
			}
			// 组装数据
			foreach ($current_hour as $key=>$value){
				// 日期数组
				$dataArr[] = $value.':00';

				// 下单量数组
				for($i=0;$i<count($moneyTotalArr);$i++){
					if($value == $moneyTotalArr[$i]['times'] ){
						$moneyArr[$key] = (float)$moneyTotalArr[$i]['xdTotalMoney'];
						break;
					}else{
						$moneyArr[$key]= 0;
					}
				}
			}
		}else{
			foreach ($current_hour as $key=>$value){
				// 日期数组
				$dataArr[] = $value.':00';
				$moneyArr[] = 0;
			}
		}

//
//		echo '<pre>';
//		print_r($moneyArr);
//		print_r($dataArr);
//		exit;

		$dataArr = json_encode($dataArr);
		$moneyArr = json_encode($moneyArr);
		$data['dataArr'] = $dataArr;
		$data['moneyArr'] = $moneyArr;

		return json_encode($data);
	}




	// 今日订单量、昨日订单量、差值
	public function getOrderToday(Request $request) {
		$day= isset($request->day) ? $request->day:'today';
		// 订单量
		$data = CurrentDay::getCurrentDayOrder($day);
		return json_encode($data);
	}
	// 今日收益额、昨日收益、差值
	public function getOrderMoneyToday(Request $request){
		$day= isset($request->day) ? $request->day:'today';
		$orderMoneyArr = CurrentDay::getCurrentDayMoney($day);
		$data['money_total'] = $orderMoneyArr['money_total'];
		$data['money_today'] = $orderMoneyArr['money_today'];
		$data['money_yestoday'] = $orderMoneyArr['money_yestoday'];

		$ordersTotal = CurrentDay::getCurrentDayOrder($day);
		$data['order_total'] = $ordersTotal['order_total'];

		// 折线图数据：开始、结束 日期数组
		$dataArr = array(); // 日期数组
		$moneyArr = array();   // 金额数组
		$orderMoneyTotal  = CurrentDay::getOrderMoneyDay();
		$current_hour = array();
		for ($i=0;$i<24;$i++){
			if($i==0){
				$current_hour[$i] = $i;
			}else{
				$current_hour[$i] = $current_hour[$i-1] + 1;
			}
		}
//			dd($current_hour);
		if($orderMoneyTotal){
			$moneyTotalArr = array(); // 折线图数据：获取数
			foreach($orderMoneyTotal as $key=>$value){
				// 拼接获取数、消耗数 相关数组
				$moneyTotalArr[$key]['times'] = isset($value->times) ? $value->times : 0;   // 时间
				$moneyTotalArr[$key]['xdTotalMoney'] = isset($value->xdTotalMoney) ? $value->xdTotalMoney : 0;   // 下单总金额
			}
			// 组装数据
			foreach ($current_hour as $key=>$value){
				// 日期数组
				$dataArr[] = $value.':00';

				// 下单量数组
				for($i=0;$i<count($moneyTotalArr);$i++){
					if($value == $moneyTotalArr[$i]['times'] ){
						$moneyArr[$key] = (float)$moneyTotalArr[$i]['xdTotalMoney'];
						break;
					}else{
						$moneyArr[$key]= 0;
					}
				}
			}
		}else{
			foreach ($current_hour as $key=>$value){
				// 日期数组
				$dataArr[] = $value.':00';
				$moneyArr[] = 0;
			}
		}

//
//		echo '<pre>';
//		print_r($moneyArr);
//		print_r($dataArr);
//		exit;

		$dataArr = json_encode($dataArr);
		$moneyArr = json_encode($moneyArr);
		$data['dataArr'] = $dataArr;
		$data['moneyArr'] = $moneyArr;

		return json_encode($data);
	}

	// 今日注册量、昨日注册量、差值
	public function getRegisterToday(Request $request){
		$day= isset($request->day) ? $request->day:'today';
		$data = CurrentDay::getCurrentDayRegister($day);
		return json_encode($data);
	}


	// 获取当前使用的网点数、名称、下单量、收入金额、 网站top5排名
	public function getBankShopOrderToday(Request $request){
		$data = array();
		$bankShopCount = BankShop::where('state','!=',-1)->count();
		$limit = isset($request->top5) ? 5:$bankShopCount;

		$bankOrderTotal = CurrentDay::getEveryBankShopOrder($limit);
//		dd($bankOrderTotal);

		foreach ($bankOrderTotal as $key=>$orderT){
			$data[$key]['id'] =$orderT->id;
			$data[$key]['shopName'] =$orderT->shopName;
			$data[$key]['state'] =$orderT->state;
			$data[$key]['order_total'] =isset($orderT->order_total) ? $orderT->order_total :0;
			$data[$key]['money_total'] =isset($orderT->money_total) ? $orderT->money_total: 0;
		}

//		dd($data);
		return json_encode($data);
	}

	// 今日订单交易详情
	public function getOrderInfoToday(Request $request){
		$num = 20;
		$cpage= isset($request->page) ? $request->page:1;//当前页
//		return $cpage;
//		dd($request->page);
		$total = CurrentDay::getCurrentDayTotal();
		$pagenum=ceil($total/$num);//总页数

		$offset=($cpage-1)*$num;//开始去数据的位置
		$data['pageTotal'] = $pagenum;
//		$num = $total;
		$data['infoList']= CurrentDay::getCurrentDayOrderInfo($offset,$num);
//		dd($data);
		return json_encode($data);
	}











	// 获取所有网点优惠券总数、使用数
	public function  getBankShopCouponTotal(){
		$data['couponTotal'] = UserCoupon::count();
		$data['couponNoUse'] = UserCoupon::where('useStatus',1)->count();
		$data['couponUsed'] = UserCoupon::where('useStatus',2)->count();
		$data['couponExpired'] = UserCoupon::where('useStatus',3)->count();
		dd($data);
		return json_encode($data);
	}


	// 获取各个网点优惠券数及使用数
	public function getEveryBankShopCoupon(){

	}



























}





